/**
 * @param {number} numCourses
 * @param {number[][]} prerequisites
 * @return {boolean}
 */
var canFinish = function(numCourses, prerequisites) {
  const inDegree = new Array(numCourses).fill(0)
     const map = {}
     for(let i = 0; i < prerequisites.length; i++) {
         inDegree[prerequisites[i][0]]++
         if(map[prerequisites[i][1]]) {
             map[prerequisites[i][1]].push(prerequisites[i][0])
         } else {
             map[prerequisites[i][1]] = [prerequisites[i][0]]
         }
     }
     const queue = []
     for(let i = 0; i < inDegree.length; i++) {
         if(inDegree[i] === 0) queue.push(i)
     }
     let count = 0
     while(queue.length) {
         const selected = queue.shift()
         count++
         const toEnqueue = map[selected]
         if(toEnqueue && toEnqueue.length) {
             for(let j = 0; j < toEnqueue.length; j++) {
                 inDegree[toEnqueue[j]]--
                 if(inDegree[toEnqueue[j]] === 0) {
                     queue.push(toEnqueue[j])
                 }
             }
         }
     }
     return count === numCourses
 };